<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/4/14 0014
 * Time: 下午 3:49
 */

namespace app\admin\controller;

use app\admin\model\AdminGroup;
use app\admin\model\Admin as AdminModel;
use app\admin\model\Menu;
use think\Db;
use think\Request;
use think\Session;

class Admin extends Common
{
    /**
     * @title   管理员列表
     * @return mixed
     */
    public function admin()
    {
        $admin = new AdminModel();
        $ag = new AdminGroup();
        $list = $admin::all()->toarray();
        foreach ($list as $key => $value){
            $list[$key]['group'] = $ag->where('id',$value['gid'])->value('title');
        }
        $this->assign('list',$list);
        return $this->fetch("admin/admin/admin");
    }

    /**
     * @title   添加管理员
     */
    public function add()
    {
        $ag = new AdminGroup();
        $select = $ag::all()->toarray();
        $this->assign('select',$select);
        return $this->fetch('admin/admin/add');
    }

    /**
     * @title   修改管理员显示页
     * @param null $id
     * @return mixed
     */
    public function edit(&$id = null)
    {
        $ag = new AdminGroup();
        $select = $ag::all()->toarray();
        $this->assign('select',$select);

        $admin = new AdminModel();
        $res = $admin::get($id)->toarray();
        $res['sel'] = $ag->field('id,title')->where('id',$res['gid'])->find()->toarray();
        $this->assign('res',$res);
        return $this->fetch('admin/admin/edit');
    }

    /**
     * @title   新增或更新管理员
     */
    public function update()
    {
        $data = Request::instance()->param();
        $data['add_time'] = time();
        $data['login_time'] = time();
        $data['login_ip'] = $_SERVER['REMOTE_ADDR'];
        $data['state'] = isset($data['state'])?$data['state']:'0';
        if(isset($data['pwd'])){
            $data['pwd'] = md5($data['pwd']);
        }
        $res = $this->store(new AdminModel(),$data);
        if($res['status']=='success'){
            $this->success($res['msg'],'admin');
        }else{
            $this->error($res['msg']);
        }
    }

    /**
     * @title   删除管理员
     * @param null $id
     */
    public function delete(&$id = null)
    {
        $admin = new AdminModel();
        $del = $admin::get($id);
        if($del->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    /**
     * @title   用户组列表
     */
    public function adminGroup()
    {
        $ag = new AdminGroup();
        $list = $ag::all()->toArray();
        $this->assign('list',$list);
        return $this->fetch('admin/admin/ag_admin');
    }

    /**
     * @title   添加用户组
     * @return mixed
     */
    public function agAdd()
    {
        //获取权限总列表
        $am = new Menu();
        $list = $am::all(function($query){
            $query->order('sort asc');
        })->toarray();
        $this->assign('rule',getMenu($list));
        return $this->fetch('admin/admin/ag_add');
    }

    /**
     * @title   修改用户组权限列表显示页
     * @param null $id  用户组id
     * @return mixed
     */
    public function agEdit(&$id = null)
    {
        //获取权限总列表
        $am = new Menu();
        $list = $am::all(function($query){
            $query->order('sort asc');
        })->toarray();
        $this->assign('rule',getMenu($list));

        //获取管理员的权限
        $ag = new AdminGroup();
        $find = $ag::get(['id'=>$id]);
        $this->assign('group',$find);
        $rule = explode(',',$find['rule']);
        $this->assign('ruleid',$rule);
        return $this->fetch('admin/admin/ag_edit');
    }

    /**
     * @title   添加用户组或修改用户组权限
     */
    public function agUpdate()
    {
        $data = Request::instance()->param();
        $data['rule'] = isset($data['rule'])?implode(',', $data['rule']):'';
        $data['state'] = input('post.state')?'1':'0';
        $res = $this->store(new AdminGroup(),$data);
        if($res['status']=='success'){
            $this->success($res['msg'],'adminGroup');
        }else{
            $this->error($res['msg']);
        }
    }

    /**
     * @title   删除用户组
     * @param null $id
     */
    public function agDelete(&$id = null)
    {
        if(!intval($id)){
            $this->error('参数错误');
        }
        $ag = new AdminGroup();
        $del = $ag::get($id);
        if($del->delete()){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }
}